"""
1.在这里输出距离矩阵
"""

import math
from scipy.io import loadmat
import numpy as np


def min_euclid_dist(A_bag,B_bag):
    """
    我只是一个算距离的工具函数
    :param A_bag:
    :param B_bag:
    :return:   实例间欧式距离min
    """
    distance = 0

    for i in range(0,len(A_bag)):
        min = 0
        for j in range(0,len(B_bag)):
            instance_a = A_bag[i]
            instance_b = B_bag[j]
            euclid_diatance= 0
            for m in range(0,len(instance_a)):
                euclid_diatance += math.pow(instance_a[m] - instance_b[m],2)
            euclid_diatance = math.sqrt(euclid_diatance)
            if(j == 0):
                min = euclid_diatance
            elif(min > euclid_diatance):
                min = euclid_diatance
        distance += min
    return distance
"""
    distance = 1

    return distance
"""
def bag_dist_matrix(musk):
    """
    :param numpy:
    :return: 包与包的距离矩阵
    """
    bag_num = len(musk['data'])
    dist_matrix = np.zeros((bag_num,bag_num))#距离矩阵

    for i in range(0,bag_num):
        for j in range(i+1,bag_num):
            euclid_a_b =min_euclid_dist(musk['data'][i][0][:,:-1],musk['data'][j][0][:,:-1])
            euclid_b_a =min_euclid_dist(musk['data'][j][0][:,:-1],musk['data'][i][0][:,:-1])
            instance_num_a = len(musk['data'][i][0])
            instance_num_b = len(musk['data'][j][0])
            distance = (euclid_a_b + euclid_b_a)/(instance_num_a+instance_num_b)

            dist_matrix[i,j] = distance

    for i in range(0,bag_num):
        for j in range(i+1,bag_num):
            dist_matrix[j][i] = dist_matrix[i][j]

    return dist_matrix

if __name__=="__main__":
    path = './musk33.mat'
    musk1 = loadmat(path)
    bag_dist =bag_dist_matrix(musk1)
    print(bag_dist)
